package cn.wolfcode.car.business.web.controller.system;

import cn.wolfcode.car.business.domain.BpmnInfo;
import cn.wolfcode.car.business.query.BpmnInfoQuery;
import cn.wolfcode.car.business.service.IBpmnInfoService;
import cn.wolfcode.car.common.base.page.TablePageInfo;
import cn.wolfcode.car.common.config.SystemConfig;
import cn.wolfcode.car.common.util.file.FileUploadUtils;
import cn.wolfcode.car.common.web.AjaxResult;
import org.apache.commons.io.FilenameUtils;
import org.apache.commons.io.IOUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;

/**
 * 客戶控制器
 */
@Controller
@RequestMapping("business/bpmnInfo")
//信息预约
public class BpmnInfoController {
    //模板前缀
    private static final String prefix = "business/system/bpmnInfo/";

    @Autowired
    private IBpmnInfoService bpmnInfoService;

    //页面------------------------------------------------------------
    //列表
    @RequestMapping("/listPage")
    public String listPage(){
        return prefix + "list";
    }

    @RequiresPermissions("system:bpmnInfo:add")
    @RequestMapping("/addPage")
    public String addPage(){
        return prefix + "add";
    }

    @RequiresPermissions("system:bpmnInfo:edit")
    @RequestMapping("/editPage")
    public String editPage(Long id, Model model){
        model.addAttribute("bpmnInfo", bpmnInfoService.get(id));
        return prefix + "edit";
    }

    //新增
    @RequiresPermissions("system:bpmnInfo:add")
    @RequestMapping("/add")
    @ResponseBody
    public AjaxResult addSave(BpmnInfo bpmnInfo){
        bpmnInfoService.save(bpmnInfo);
        return AjaxResult.success();
    }

    //编辑
    @RequiresPermissions("system:bpmnInfo:edit")
    @RequestMapping("/edit")
    @ResponseBody
    public AjaxResult edit(BpmnInfo bpmnInfo){
        bpmnInfoService.update(bpmnInfo);
        return AjaxResult.success();
    }

    @RequestMapping("/query")
    @ResponseBody
    //分页
    public TablePageInfo<BpmnInfo> query(BpmnInfoQuery qo){
        return bpmnInfoService.query(qo);
    }

    @RequestMapping("/deployPage")
    //deployPage(部署页面)
    public String deployPage(Model model,Long id){
        model.addAttribute("bpmnInfo", bpmnInfoService.get(id));
        return prefix + "deploy";
    }

    //上传流程文件
    @RequiresPermissions("system:bpmnInfo:upload")
    @RequestMapping("/upload")
    @ResponseBody
    public AjaxResult upload(MultipartFile file) throws IOException {
        if(file != null && file.getSize() >0){
            //获取文件名
            String originalFilename = file.getOriginalFilename();
            //获取文件的后缀名
            String extName = FilenameUtils.getExtension(originalFilename);
            //判断bpmn文件名是否相等
            if("bpmn".equalsIgnoreCase(extName)){
                //path 路径 upload 上传
                System.out.println(SystemConfig.getUploadPath());
                System.out.println(file);
                String uploadPath = FileUploadUtils.upload(SystemConfig.getUploadPath(), file);
                System.out.println(uploadPath);
                return AjaxResult.success("上传成功",uploadPath);
            }else {
                return AjaxResult.error("流程定义文件仅支持 bpmn格式");
            }
        }else {
            return AjaxResult.error("不允许上传空文件");
        }
    }

    //部署逻辑
    @RequiresPermissions("system:bpmnInfo:deploy")
    @RequestMapping("/deploy")
    @ResponseBody
    //bpmnInfoPath (bpmn信息路径)
    public AjaxResult deploy(String path,String bpmnType,String info) throws FileNotFoundException {
        bpmnInfoService.deploy(path,bpmnType,info);
        return AjaxResult.success();
    }

    //实现流程定义文件删除
    @RequiresPermissions("system:bpmnInfo:delete")
    @RequestMapping("/delete")
    @ResponseBody
    public AjaxResult delete(Long id){
        bpmnInfoService.delete(id);
        return AjaxResult.success();
    }

    //实现查看资源
    @RequiresPermissions("system:bpmnInfo:readResource")
    @RequestMapping("/readResource")
    @ResponseBody
    //deploymentId(部署ID)
    public void readResource(String deploymentId, String type , HttpServletResponse response) throws IOException {
        InputStream inputStream = bpmnInfoService.getResoucceByType(deploymentId,type);
        IOUtils.copy(inputStream,response.getOutputStream());
    }
}
